﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;
using System.Threading;
using XChipMatlab;
using XChip_Common;
using XChipTool.Enties;

namespace XChipTool
{
    public partial class XChip_DETAIL : DevExpress.XtraEditors.XtraUserControl
    {

        public SerialPort sPortTemp;
        private bool isRead = false;
        private DevExpress.Utils.WaitDialogForm WaitBeforeLogin = null;
        private KeyEventArgs keyEnter = new KeyEventArgs(Keys.Enter);//用来手动触发文本框下发配置
        private bool isWrite = false;

        List<string> listfiltration = new List<string> { "scale_sig", "slope", "offset" };
        public XChip_DETAIL()
        {
            InitializeComponent();
        }
        private long GetDetailRegister(string nameType)
        {

            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return -1;
            }
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == nameType.ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == nameType.ToLower());
                var Val = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                if (model.ControlType.Contains("1"))
                {
                    return BitsHelp.GetBitsCheckedToBool(model.Bits, model.Hex, Val.ToString("X2"));
                }
                else
                {
                    return BitsHelp.GetBitsToUint(model.Bits, model.Hex, Val.ToString("X2"));
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1;
            }
        }
       
        private void simpleButton5_Click(object sender, EventArgs e)
        {
           
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            WaitBefore.Start("读取中", "正在加载中", 10);
            isRead = true;
            

            string nameType = string.Empty;
            for (int i = 0; i < XChipHelp.XChipDetail.Count; i++)
            {

                var type = AppHelper.NameTypeToString(XChipHelp.XChipDetail[i].NameType);
               
                try
                {

                    Control col = this.Controls.Find(type, true)[0];
                    if (nameType == type) continue;
                    nameType = type;

                    if (col is TextEdit)
                    {
                        //过滤掉指定寄存器
                        if (listfiltration.Exists(c => c == col.Name.ToString()))
                        {
                            continue;
                        }
                        var nameList = XChipHelp.XChipDetail.Where(c => c.NameType.ToLower().Contains(type)).ToList();
                        long tempU = 0;
                        foreach (var nitem in nameList)
                        {
                            tempU += GetDetailRegister(nitem.NameType);
                        }
                        col.Text = tempU.ToString("X");
                    }
                    else if (col is CheckEdit)
                    {
                        (col as CheckEdit).Checked = (GetDetailRegister(type) == 0);
                    }
                }
                catch (Exception)
                {
                    continue;
                }

            }
            isRead = false;
            WaitBefore.Close();
        }

        private long GetAERegister(string nameType)
        {
            if (XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
                return SerialSend.ReadRegister(Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), sPortTemp);
            else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1; }
        }
        void  HGAINEN_IsEnable(bool isbool)
        {
            Hgain_0.Enabled = isbool;
            Hgain_1.Enabled = isbool;
            Hgain_2.Enabled = isbool;
            Hgain_3.Enabled = isbool;
            Hgain_4.Enabled = isbool;
            Hgain_5.Enabled = isbool;
            Thr_0.Enabled = isbool;
            Thr_1.Enabled = isbool;
            Thr_2.Enabled = isbool;
            Thr_3.Enabled = isbool;
        }


        private void SetDetailRegister(addressregister model, ulong add, bool isCheck = true)
        {
            if (!sPortTemp.IsOpen)//串口检测
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("串口未打开");
                return;
            }
            if (isRead) return;

            SerialSend.WriteRegister(Convert.ToUInt32(model.Address, 16), add, sPortTemp);
        }

        private void MIN_DETAIL_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (string.IsNullOrEmpty(min_detail.Text)) return;
            //SetDetailRegister("MIN_DETAIL", (ulong)Convert.ToInt32(min_detail.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(min_detail.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"min_detail").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"min_detail").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, min_detail.Text);
                SetDetailRegister(model, Val);
            }

        }

        private void MAX_DETAIL_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (string.IsNullOrEmpty(max_detail.Text)) return;
            //SetDetailRegister("MAX_DETAIL", (ulong)Convert.ToInt32(max_detail.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(max_detail.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"max_detail").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"max_detail").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, max_detail.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void MIN_SHP_GAIN_Leave(object sender, EventArgs e)
        {
            
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(min_shp_gain.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"min_shp_gain").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"min_shp_gain").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, min_shp_gain.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void MAX_SHP_GAIN_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (string.IsNullOrEmpty(max_shp_gain.Text)) return;
            //SetDetailRegister("MAX_SHP_GAIN", (ulong)Convert.ToInt32(max_shp_gain.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(max_shp_gain.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"max_shp_gain").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"max_shp_gain").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, max_shp_gain.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Thr_0_Leave(object sender, EventArgs e)
        {
            
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Thr_0.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Thr_0").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Thr_0").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Thr_0.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Thr_1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Thr_1.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Thr_1").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Thr_1").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Thr_1.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Thr_2_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //if (string.IsNullOrEmpty(Thr_2.Text)) return;
            //SetDetailRegister("Thr_2", (ulong)Convert.ToInt32(Thr_2.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Thr_2.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Thr_2").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Thr_2").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Thr_2.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Thr_3_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Thr_3.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Thr_3").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Thr_3").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Thr_3.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Hgain_0_Leave(object sender, EventArgs e)
        {
            
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Hgain_0.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_0").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_0").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Hgain_0.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Hgain_1_Leave(object sender, EventArgs e)
        {
            
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Hgain_1.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_1").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_1").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Hgain_1.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Hgain_2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Hgain_2.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_2").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_2").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Hgain_2.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Hgain_3_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Hgain_3.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_3").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_3").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Hgain_3.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Hgain_4_Leave(object sender, EventArgs e)
        {

            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Hgain_4.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_4").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_4").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Hgain_4.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void Hgain_5_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Hgain_5.Text)) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_5").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_5").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Hgain_5.Text);
                SetDetailRegister(model, Val);
            }
        }

        private void MIN_DETAIL_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MIN_DETAIL_Leave(sender, null);
            }
        }

        private void MAX_DETAIL_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MAX_DETAIL_Leave(sender, null);
            }
        }

        private void MIN_SHP_GAIN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MIN_SHP_GAIN_Leave(sender, null);
            }
        }

        private void MAX_SHP_GAIN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MAX_SHP_GAIN_Leave(sender, null);
            }
        }

        private void Thr_0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Thr_0_Leave(sender, null);
            }
        }

        private void Thr_1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Thr_1_Leave(sender, null);
            }
        }

        private void Thr_2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Thr_2_Leave(sender, null);
            }
        }

        private void Thr_3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Thr_3_Leave(sender, null);
            }
        }

        private void Hgain_0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Hgain_0_Leave(sender, null);
            }
        }

        private void Hgain_1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Hgain_1_Leave(sender, null);
            }
        }

        private void Hgain_2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Hgain_2_Leave(sender, null);
            }
        }

        private void Hgain_3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Hgain_3_Leave(sender, null);
            }
        }

        private void Hgain_4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Hgain_4_Leave(sender, null);
            }
        }

        private void Hgain_5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Hgain_5_Leave(sender, null);
            }
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            //string str = "       Yedge调节细节的增强程度，edge_ratio 值越大，增强越明显；当 Cur_ pixel 点的 Y \r\n       值与相邻区域均值差异＞diff 设定值时，会判断为边缘点，可调节 sharpen_up 进一步增强；差异＜diff 设定值时，会判断为非边缘点，可调节 sharpen_down 进行相应减弱，其中 0x20为基准一倍，设定值＞0x20则增强，设定值＜0x20则减弱。\r\n       可通过设置Threshold阈值对画面的细节边缘程度进行分段，分别设置不同区段的edge_ratio权重值，其中权重值的取值范围为 0x00~0x05。\r\n       Hgain_en使能后，也可通过设置thr阈值对画面小细节程度进行分段，分别设置不同区段的hgain增益值，其中0x10为基准一倍，设定值＞0x10则增强，设定值＜0x10则减弱。\r\n       更多详见《调试工具用户手册》。";
            Thread thread = new Thread(threadPro);
            thread.Start();
        }
        private void threadPro()

        {

            MethodInvoker methodInvoker = new MethodInvoker(ShowFormB);

            BeginInvoke(methodInvoker);

        }

        private void ShowFormB()

        {
            //string str = "\t      \t Yedge调节细节的增强程度；\r\n"
            //    + "\t      \t 调整梯形控制函数Control_shoot区分平坦区、小细节、大细节等区段及区段细节增强的程度，shoot_x_shift调整不同增益下的X轴偏移量；scale_lap_enh调整不同增益下的增强倍率；\r\n"
            //    + "\t      \t Thr_slope和 thr_offset是阈值控制设定值，可根据当前点的亮度，得到当前点的阈值 Thr，对 diff >Thr 区域进行增强，thr_slope/thr_offset值越小，增强区域越多。\r\n"
            //    + "\t      \t Hgain_en使能后，也可通过设置thr阈值对画面小细节程度进行分段，分别设置不同区段的hgain增益值，其中0x10为基准一倍，设定值＞0x10则增强，设定值＜0x10则减弱。\r\n"
            //    + "\t      \t 更多详见《调试工具用户手册》。\r\n";
            string str = "";
            XChip_OptimizationTips ot = new XChip_OptimizationTips("DETAIL调优Tips", str);
            ot.StartPosition = FormStartPosition.CenterScreen;
            //ot.ShowDialog();
            ot.Show();

        }

        private void shoot_x_node_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    TextEdit txt = sender as TextEdit;
            //    if (string.IsNullOrEmpty(txt.Text)) return;
            //    uint sendVal = Convert.ToUInt32(txt.Text, 16);
            //    SetDetailRegister("shoot_x_node" + txt.Tag.ToString(), sendVal);               
            //}
        }

        private void shoot_y_node_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    TextEdit txt = sender as TextEdit;
            //    if (string.IsNullOrEmpty(txt.Text)) return;
            //    uint sendVal = Convert.ToUInt32(txt.Text, 16);
            //    SetDetailRegister("shoot_y_node" + txt.Tag.ToString(), sendVal);
            //}
        }

        private void txt_scale_lap_enh_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
            edge_ratio_day_KeyDown(sender, e); // 下发同时下发
        }

        private void txt_thr_slope_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void txt_thr_offset_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void txt_shoot_x_shift_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void scale_lap_enh_manua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //TextEdit txt = sender as TextEdit;
                //if (string.IsNullOrEmpty(txt.Text)) return;
                //uint sendVal = Convert.ToUInt32(txt.Text, 16);
                //SetDetailRegister("scale_lap_enh_man", sendVal);

                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                if (string.IsNullOrEmpty(scale_lap_enh_man.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"scale_lap_enh_man").ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"scale_lap_enh_man").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_lap_enh_man.Text);
                    SetDetailRegister(model, Val);
                }

            }
        }

        private void thr_slope_manua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //TextEdit txt = sender as TextEdit;
                //if (string.IsNullOrEmpty(txt.Text)) return;
                //uint sendVal = Convert.ToUInt32(txt.Text, 16);
                //SetDetailRegister("thr_slope_man", sendVal);
                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                if (string.IsNullOrEmpty(thr_slope_man.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"thr_slope_man").ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"thr_slope_man").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, thr_slope_man.Text);
                    SetDetailRegister(model, Val);
                }
            }
        }

        private void thr_offset_manua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 
                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                if (string.IsNullOrEmpty(thr_offset_man.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"thr_offset_man").ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"thr_offset_man").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, thr_offset_man.Text);
                    SetDetailRegister(model, Val);
                }
            }
        }

        private void shoot_x_shift_manua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 
                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                if (string.IsNullOrEmpty(shoot_x_shift_man.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"shoot_x_shift_man").ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"shoot_x_shift_man").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, shoot_x_shift_man.Text);
                    SetDetailRegister(model, Val);
                }
            }
        }

        private void scale_lap_enh_manua_en_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //CheckEdit chk = sender as CheckEdit;
            //uint readVal = (uint)GetDetailRegister("scale_lap_enh_man_en");
            //scale_lap_enh_man.Enabled = chk.Checked;
            //if (chk.Checked)
            //{
            //    SetDetailRegister("scale_lap_enh_man_en", readVal | 0x01);
               
            //}
            //else
            //{
            //    SetDetailRegister("scale_lap_enh_man_en", readVal & 0xfe);
            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"scale_lap_enh_man_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"scale_lap_enh_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), scale_lap_enh_man_en.Checked);
                SetDetailRegister(model, Val);
            }
        }

        private void thr_slope_manua_en_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //CheckEdit chk = sender as CheckEdit;
            //uint readVal = (uint)GetDetailRegister("thr_slope_man_en");
            //thr_slope_man.Enabled = chk.Checked;
            //if (chk.Checked)
            //{
            //    SetDetailRegister("thr_slope_man_en", readVal | 0x01);
            //}
            //else
            //{
            //    SetDetailRegister("thr_slope_man_en", readVal & 0xfe);
            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"thr_slope_man_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"thr_slope_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), thr_slope_man_en.Checked);
                SetDetailRegister(model, Val);
            }
        }

        private void thr_offset_manua_en_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //CheckEdit chk = sender as CheckEdit;
            //uint readVal = (uint)GetDetailRegister("thr_offset_man_en");
            //thr_offset_man.Enabled = chk.Checked;
            //if (chk.Checked)
            //{
            //    SetDetailRegister("thr_offset_man_en", readVal | 0x01);
            //}
            //else
            //{
            //    SetDetailRegister("thr_offset_man_en", readVal & 0xfe);
            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"thr_offset_man_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"thr_offset_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), thr_offset_man_en.Checked);
                SetDetailRegister(model, Val);
            }
        }

        private void shoot_x_shift_manua_en_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //CheckEdit chk = sender as CheckEdit;
            //uint readVal = (uint)GetDetailRegister("shoot_x_shift_man_en");
            //shoot_x_shift_man.Enabled = chk.Checked;
            //if (chk.Checked)
            //{
            //    SetDetailRegister("shoot_x_shift_man_en", readVal | 0x01);
            //}
            //else
            //{
            //    SetDetailRegister("shoot_x_shift_man_en", readVal & 0xfe);
            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"shoot_x_shift_man_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"shoot_x_shift_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), shoot_x_shift_man_en.Checked);
                SetDetailRegister(model, Val);
            }
        }

        private void edge_ratio_day_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (false)
                    {
                        
                    }
                    else
                    {
                        if (isRead) return;
                        var txt = sender as DevExpress.XtraEditors.TextEdit;
                        if (string.IsNullOrEmpty(txt.Text)) return;
                        if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                        {
                            var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                            var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                            SetDetailRegister(model, Val);
                        }
                    }
                }
            }
        }
       

        private void cip_noise_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void min_sharpen_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void max_sharpen_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void chk_IQSW_CheckedChanged(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDetailRegister(model, Val);
                }
            }
            //edge_ratio_IsEnable(chk.Checked);
        }

        private void chk_MonoMode_CheckedChanged(object sender, EventArgs e)
        {
            //DayMonoSwitchLable(chk_Mono_mode.Checked);
            if (isWrite) return;
            if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDetailRegister(model, Val);
                }
            }
        }
      
        private void HGAIN_EN_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //uint readVal = (uint)GetDetailRegister("HGAIN_EN");
            //if (Hgain_en.CheckState == CheckState.Checked)
            //{
            //    SetDetailRegister("HGAIN_EN", (ulong)(readVal | 0x08));              
            //}
            //else
            //{
            //    SetDetailRegister("HGAIN_EN", (ulong)(readVal & 0xf7));

            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"Hgain_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"Hgain_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Hgain_en.Checked);
                SetDetailRegister(model, Val);
            }
            HGAINEN_IsEnable(Hgain_en.Checked);
        }
        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
           
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (string.IsNullOrEmpty(shoot_x_node_x0.Text) ||
               string.IsNullOrEmpty(shoot_x_node_x1.Text) ||
               string.IsNullOrEmpty(shoot_x_node_x2.Text) ||
               string.IsNullOrEmpty(shoot_x_node_x3.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("X参数缺省！");
                return;
            }
            if (string.IsNullOrEmpty(shoot_y_node_y0.Text) ||
                string.IsNullOrEmpty(shoot_y_node_y1.Text) ||
                string.IsNullOrEmpty(shoot_y_node_y2.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Y参数缺省！");
                return;
            }

            try
            {
               
                var x0 =  Convert.ToInt32(shoot_x_node_x0.Text,16);
                var x1 = Convert.ToInt32(shoot_x_node_x1.Text, 16);
                var x2 = Convert.ToInt32(shoot_x_node_x2.Text, 16);
                var x3 = Convert.ToInt32(shoot_x_node_x3.Text, 16);

                var y0 = Convert.ToInt32(shoot_y_node_y0.Text, 16);
                var y1 = Convert.ToInt32(shoot_y_node_y1.Text, 16);
                var y2 = Convert.ToInt32(shoot_y_node_y2.Text, 16);

                if ((x0>x1)||(x1>x2)||(x2>x3))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("检查：x值按照小到大递增排序");
                    return;
                }
                var shoot_y_slope_lf = Math.Round((y1 - y0) * 256.0m / (x1 - x0));
                var shoot_y_slope_rt = Math.Round((y1 - y2) * 256.0m / (x3 - x2));
                var shoot_y_slope = new decimal[] { shoot_y_slope_lf, shoot_y_slope_rt };
                if (0 >= shoot_y_slope_lf || shoot_y_slope_lf > 8192)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("检查：0 大于等于 (y2-y1)*256 / x2-x1 小于 0x2000");
                    return;
                }
                if (0 >= shoot_y_slope_rt || shoot_y_slope_rt > 8192)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("检查：0 大于等于 (y2-y3)*256 / x4-x3 小于 0x2000");
                    return;
                }
                Task.Run(() => {
                    WaitBeforeLogin = new DevExpress.Utils.WaitDialogForm("请稍候...", "正在加载yedge应用系统");
                    WaitBeforeLogin.StartPosition = FormStartPosition.CenterParent;
                    Application.Run(WaitBeforeLogin);
                });
                btnSend.Enabled = false;

                var shoot_x_node = new decimal[] { x0, x1, x2, x3 };
                var shoot_y_node = new decimal[] { y0, y1, y2 };
                YedgeHelp ye = new YedgeHelp();
             
                List<ISPReg> regList = new List<ISPReg>();
                regList.Add(new ISPReg() { addr = Convert.ToUInt32("fffd", 16) ,value = Convert.ToUInt32("80", 16) });
                regList.Add(new ISPReg() { addr = Convert.ToUInt32("fffe", 16), value = Convert.ToUInt32("30", 16) });
                regList.AddRange(ye.para_to_reg(shoot_x_node, 8, "3636"));
                regList.AddRange(ye.para_to_reg(shoot_y_node, 8, "363a"));
                regList.AddRange(ye.para_to_reg(shoot_y_slope, 13, "363d"));
               
                uint addrBHigh = 0x80;
                    uint addrMid = 0;
                    foreach (var item in regList)
                    {
                        if (item.addr == 0xffff)
                            continue;
                        else if (item.addr == 0xfffd)
                        {
                            addrBHigh = item.value;
                            continue;
                        }
                        else if (item.addr == 0xfffe)
                        {
                            addrMid = item.value;
                            continue;
                        }
                        SerialSend.WriteRegister(((addrBHigh << 24) + (addrMid << 16) + item.addr), item.value, sPortTemp);
                    }
                    btnSend.Invoke((EventHandler)delegate { btnSend.Enabled = true; });
            }
            catch (Exception ex)
            {
                btnSend.Invoke((EventHandler)delegate { btnSend.Enabled = true; });
                DevExpress.XtraEditors.XtraMessageBox.Show("错误信息：" + ex.Message.ToString());
            }
            finally
            {
                WaitBeforeLogin.Invoke((EventHandler)delegate { WaitBeforeLogin.Close(); });
            }
        }

        private void scale_lap_enh_1x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_scale_lap_enh_KeyDown(sender, keyEnter);
        }

        private void thr_slope_1x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_thr_slope_KeyDown(sender, keyEnter);
        }

        private void thr_offset_1x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_thr_offset_KeyDown(sender, keyEnter);
        }

        private void shoot_x_shift_1x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_shoot_x_shift_KeyDown(sender, keyEnter);
        }

        private void scale_lap_enh_manua_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_lap_enh_manua_KeyDown(sender, keyEnter);
        }

        private void thr_slope_manua_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            thr_slope_manua_KeyDown(sender, keyEnter);
        }

        private void thr_offset_manua_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            thr_offset_manua_KeyDown(sender, keyEnter);
        }

        private void shoot_x_shift_manua_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            shoot_x_shift_manua_KeyDown(sender, keyEnter);
        }

        private void txt_cip_noise_day_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            cip_noise_KeyDown(sender, keyEnter);
        }

        private void txt_cip_noise_night_Leave(object sender, EventArgs e)
        {
            cip_noise_KeyDown(sender, keyEnter);
        }

        private void txt_min_shp_day_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            min_sharpen_KeyDown(sender, keyEnter);
        }

        private void txt_min_shp_night_Leave(object sender, EventArgs e)
        {
            min_sharpen_KeyDown(sender, keyEnter);
        }

        private void txt_max_shp_day_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            max_sharpen_KeyDown(sender, keyEnter);
        }

        private void txt_max_shp_night_Leave(object sender, EventArgs e)
        {
            max_sharpen_KeyDown(sender, keyEnter);
        }

        private void btn_Refresh_Click_1(object sender, EventArgs e)
        {
            SerialSend.WriteRegister(0x80140a8f, 0x01, sPortTemp);
        }

        private void SetCmxRegister(string nameType, ulong add)
        {
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (isRead) return;
            if (XChipHelp.XChipCmx.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
            {
                SerialSend.WriteRegister(Convert.ToUInt32(XChipHelp.XChipCmx.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), add, sPortTemp);
            }
        }
        private long GetCMXRegister(string nameType)
        {
            if (XChipHelp.XChipCmx.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
                return SerialSend.ReadRegister(Convert.ToUInt32(XChipHelp.XChipCmx.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), sPortTemp);
            else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1; }
        }
        private void checkedListBoxControl1_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (isRead) return;

            //Scence Switch En 
            var nTemp1 = GetCMXRegister("nEffectSceneMode");
            // Debug en
            var nTemp2 = GetCMXRegister("nSwitchMode");
            if (checkedListBoxControl1.Items[0].CheckState == CheckState.Checked)//S勾选
            {
                SetCmxRegister("nEffectSceneMode", (ulong)(nTemp1 | 0x04));
                checkedListBoxControl1.Items[1].Enabled = true;
                if (checkedListBoxControl1.Items[1].CheckState == CheckState.Checked)//D勾选
                {
                    rdo_scene.Enabled = true;
                }
            }
            else  //S不勾选
            {
                SetCmxRegister("nEffectSceneMode", (ulong)(nTemp1 & 0xfb));
                checkedListBoxControl1.Items[1].Enabled = false;
                rdo_scene.Enabled = false;
            }


            if (checkedListBoxControl1.Items[1].CheckState == CheckState.Checked)//D勾选
            {
                //
               
                SetCmxRegister("nSwitchMode", (ulong)(nTemp2 & 0xfd));
                if (checkedListBoxControl1.Items[0].CheckState == CheckState.Checked)//S勾选
                {
                    rdo_scene.Enabled = true;
                }
            }
            else//D不勾选
            { //
               
                SetCmxRegister("nSwitchMode", (ulong)(nTemp2 | 0x02));
                rdo_scene.Enabled = false;
            }
        }

        private void rdo_scene_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (isRead) return;
            var aeradio = sender as DevExpress.XtraEditors.RadioGroup;
           
            if (aeradio.EditValue.ToString() == "1")
            {
                SetCmxRegister("cur_scene_nIdx", 0x00);
            }
            else if (aeradio.EditValue.ToString() == "2")
            {
                SetCmxRegister("cur_scene_nIdx", 0x01);
            }
            else if (aeradio.EditValue.ToString() == "3")
            {
                SetCmxRegister("cur_scene_nIdx", 0x02);
            }
            Thread.Sleep(50);
            SerialSend.WriteRegister(0x80140a8f, 0x01, sPortTemp);
        }

        private void scale_med_lap_1x_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void scale_med_lap_1x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_med_lap_1x_KeyDown(sender, keyEnter);
        }

        private void scale_med_enh_1x_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (null != sPortTemp && sPortTemp.IsOpen)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDetailRegister(model, Val);
                    }
                }
            }
        }

        private void scale_med_enh_1x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_med_enh_1x_KeyDown(sender, keyEnter);
        }

        private void scale_med_lap_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                if (string.IsNullOrEmpty(scale_med_lap_man.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"scale_med_lap_man").ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"scale_med_lap_man").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_med_lap_man.Text);
                    SetDetailRegister(model, Val);
                }
            }
        }

        private void scale_med_lap_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_med_lap_man_KeyDown(sender, keyEnter);
        }

        private void scale_med_enh_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                if (string.IsNullOrEmpty(scale_med_enh_man.Text)) return;
                if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"scale_med_enh_man").ToLower()))
                {
                    var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"scale_med_enh_man").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_med_lap_man.Text);
                    SetDetailRegister(model, Val);
                }
            }
        }

        private void scale_med_enh_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_med_enh_man_KeyDown(sender, keyEnter);
        }

        private void scale_med_lap_man_en_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //CheckEdit chk = sender as CheckEdit;
            //uint readVal = (uint)GetDetailRegister("scale_med_lap_man_en");
            //scale_med_lap_man.Enabled = chk.Checked;
            //if (chk.Checked)
            //{
            //    SetDetailRegister("scale_med_lap_man_en", readVal | 0x01);

            //}
            //else
            //{
            //    SetDetailRegister("scale_med_lap_man_en", readVal & 0xfe);
            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"scale_med_lap_man_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"scale_med_lap_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), scale_med_lap_man_en.Checked);
                SetDetailRegister(model, Val);
            }
        }

        private void scale_med_enh_man_en_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //CheckEdit chk = sender as CheckEdit;
            //uint readVal = (uint)GetDetailRegister("scale_med_enh_man_en");
            //scale_med_enh_man.Enabled = chk.Checked;
            //if (chk.Checked)
            //{
            //    SetDetailRegister("scale_med_enh_man_en", readVal | 0x01);

            //}
            //else
            //{
            //    SetDetailRegister("scale_med_enh_man_en", readVal & 0xfe);
            //}
            if (isRead) return;
            if (XChipHelp.XChipDetail.Exists(c => c.NameType.ToLower() == ($"scale_med_enh_man_en").ToLower()))
            {
                var model = XChipHelp.XChipDetail.Find(c => c.NameType.ToLower() == ($"scale_med_enh_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), scale_med_enh_man_en.Checked);
                SetDetailRegister(model, Val);
            }
        }

        private void checkEdit1_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            if (checkEdit1.CheckState == CheckState.Checked)
            {
                SerialSend.WriteRegister(0x8030364d, 0x1, sPortTemp);
                panelControl7.Visible = true;
            }
            else
            {
                SerialSend.WriteRegister(0x8030364d, 0x0, sPortTemp);
                panelControl7.Visible = false;
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var val = Convert.ToUInt32(radioGroup1.Properties.Items[radioGroup1.SelectedIndex].Description.ToString() ,16);
            SerialSend.WriteRegister(0x8030364e, val, sPortTemp);
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            //SerialSend.isDebug = false;
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "配置文件（*.txt）|*.txt";

            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            sfd.FileName = "IQ_Setting_DETAIL_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                var Pe_dal = new parameter_exportManager();
                if (null != Pe_dal)
                {
                    WaitBefore.Start("参数导出", "正在加载中", 60);
                    //var tempList = Pe_dal.GetList(c => c.Module == "AWB");
                    var tempList = Pe_dal.GetListToExport().Where(c => c.Module == "Y edge" || 
                    c.Module.Contains("cip_noise 0x80300f00") || 
                    c.Module.Contains("cip_shp 0x80300f0a") ||
                    c.Module.Contains("yedge_ratio 0x80303612")).OrderBy(c => c.SortNum).ToList();
                    #region 判断是否需要用白天的数据
                    var Pe_patch_dal = new parameter_export_patchManager();
                    var patchList = Pe_patch_dal.GetList();
                    var conditionList = tempList.Select(c => c.ConditionAddr).Distinct();
                    foreach (var item in conditionList)
                    {
                        if (SerialSend.ReadRegister(Convert.ToUInt32(item, 16), sPortTemp) == 0)
                        {
                            patchList.RemoveAll(c => c.ConditionAddr == item);
                        }
                    }
                    #endregion
                    var sb = XChipHelp.ReadParameterExportExpand(tempList, "80", patchList, sPortTemp);
                    System.IO.File.WriteAllText(sfd.FileName, sb.ToString());

                }
                WaitBefore.Close();
                DevExpress.XtraEditors.XtraMessageBox.Show("参数导出完成");
            }
        }

        private void simpleButton5_MouseEnter(object sender, EventArgs e)
        {
            isWrite = true;
        }

        private void simpleButton5_MouseLeave(object sender, EventArgs e)
        {
            isWrite = false;
        }

        private void XChip_DETAIL_Load(object sender, EventArgs e)
        {
            Task.Run(() => {
                this.Invoke((EventHandler)delegate {
                    for (int i = 0; i < XChipHelp.XChipDetail.Count; i++)
                    {

                        var type = AppHelper.NameTypeToString(XChipHelp.XChipDetail[i].NameType);

                        try
                        {
                            var col = this.Controls.Find(type, true);


                            foreach (var item in col)
                            {
                                    if (item is TextEdit)
                                    {
                                        //声明一个SuerToolTip
                                        DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                                        //声明一个ToolTipTitleItem,保存标题
                                        DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                                        String tooltips = "";

                                        if (!string.IsNullOrEmpty(XChipHelp.XChipDetail[i].Bit_Wide))
                                        {
                                            tooltips += $"位宽={XChipHelp.XChipDetail[i].Bit_Wide}";
                                        }
                                        if (!string.IsNullOrEmpty(XChipHelp.XChipDetail[i].Bit_Accuracy))
                                        {
                                            tooltips += $"|精度={XChipHelp.XChipDetail[i].Bit_Accuracy}";
                                        }
                                        //设置标题文字
                                        toolTipTitleItem.Text = tooltips;
                                        //把标题添加到SuerToolTip
                                        graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                                        //指定控件的SuperTip
                                        ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                                        //正则判断
                                        ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                                        ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipHelp.XChipDetail[i].Bits, XChipHelp.XChipDetail[i].Bit_Wide, XChipHelp.XChipDetail.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());
                                    }
                                }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                });
            });
        }
    }
}
